// 获取应用实例
const app = getApp()

Page({
	data: {
		img_src:app.globalData.img_src,
		popshow: false,
		date: '',
		checkBox:[],
		clickable:true,
		userinfo:null
	},
	onLoad(){
		wx.hideHomeButton()
		var userinfo = wx.getStorageSync('memberinfo')
		this.setData({
			userinfo:userinfo
		})
	},
	rulePop: function() {
		this.setData({
			popshow: true
		})
	},
	closeBtn: function() {
		this.setData({
			popshow: false
		})
	},
	formSubmit(e) {
		var userinfo = wx.getStorageSync('memberinfo')
		if(this.data.clickable == false){
			return false;
		}
		this.setData({clickable:false})
   	var that = this;
		setTimeout(function(){
			that.setData({clickable:true})
		},2000)

		if(e.detail.value.name == ''){
			wx.showToast({
				title:'请输入姓名',
				icon:'none',
				duration:2000
			})
			return
		}
		if(e.detail.value.tel == ''){
			wx.showToast({
				title:'请输入手机号',
				icon:'none',
				duration:2000
			})
			return
		}
		if (e.detail.value.tel.trim().length != 11 || !(/^1[3456789]\d{9}$/.test(e.detail.value.tel))) {
			wx.showToast({
				title:'手机号格式不正确',
				icon:'none',
				duration:2000
			})
			return
		}
		if(e.detail.value.date == ''){
			wx.showToast({
				title:'请选择生日',
				icon:'none',
				duration:2000
			})
			return
		}
		
		var minutes = 1000 * 60;
		var hours = minutes * 60;
		var days = hours * 24;
		var years = days * 365;
		var t = Date.now();
		var b = Date.parse(e.detail.value.date);
		if(t - b < 14*years){
			wx.showToast({
				title:'您未年满14周岁，不符合注册条件',
				icon:'none',
				duration:2000
			})
			return
		}

		if(this.data.checkBox.indexOf("cb") == "-1"){
			wx.showToast({
				title:'请阅读并同意个人信息处理规则',
				icon:'none',
				duration:2000
			})
			return
		}
		if(this.data.checkBox.indexOf("cb2") == "-1"){
			wx.showToast({
				title:'请阅读并同意个人信息处理规则',
				icon:'none',
				duration:2000
			})
			return
		}
		var posdata = {
			invite_id:wx.getStorageSync('invite_id'),
			name:e.detail.value.name,
			mobile:e.detail.value.tel,
			birthday:e.detail.value.date,
			openid: userinfo.openid
		}
		wx.request({
			url: app.globalData.apiurl+'access.php?a=signup', 
			header: {
				"Content-Type": "application/x-www-form-urlencoded"
			},
			data: posdata,
			method:"POST",
			success (res) {
				console.log(res);
				return;
				if(res.data.status == 1){
					if(res.data.data.hkgift){
						wx.redirectTo({
							url: '/pages/result/result?type=succ'
						})
					}else{
						wx.redirectTo({
							url: '/pages/result/result?type=succ2'
						})
					}
					
				}else{
					wx.showToast({
						title: res.data.info,
						icon:'none'
					})
				}
				
			}
		})

		
	},
	bindDateChange: function(e) {
		console.log('picker发送选择改变，携带值为', e.detail.value)
		this.setData({
			date: e.detail.value
		})
	},
	checkboxChange(e){
		this.setData({
			checkBox:e.detail.value
		})
	}
})
